package com.ruoyi.hotel.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hotel.domain.Room;
import com.ruoyi.hotel.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.mapper.HotelOrderMapper;
import com.ruoyi.hotel.domain.HotelOrder;
import com.ruoyi.hotel.service.IHotelOrderService;

/**
 * 订单Service业务层处理
 *
 * @author w
 * @date 2025-03-21
 */
@Service
public class HotelOrderServiceImpl implements IHotelOrderService
{
    @Autowired
    private HotelOrderMapper hotelOrderMapper;

    @Autowired
    private RoomMapper roomMapper;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public HotelOrder selectHotelOrderById(Long id)
    {
        return hotelOrderMapper.selectHotelOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param hotelOrder 订单
     * @return 订单
     */
    @Override
    public List<HotelOrder> selectHotelOrderList(HotelOrder hotelOrder)
    {
        return hotelOrderMapper.selectHotelOrderList(hotelOrder);
    }

    /**
     * 新增订单
     *
     * @param hotelOrder 订单
     * @return 结果
     */
    @Override
    public int insertHotelOrder(HotelOrder hotelOrder)
    {
        hotelOrder.setCreateTime(DateUtils.getNowDate());
        return hotelOrderMapper.insertHotelOrder(hotelOrder);
    }

    /**
     * 修改订单
     *
     * @param hotelOrder 订单
     * @return 结果
     */
    @Override
    public int updateHotelOrder(HotelOrder hotelOrder)
    {
        int i  = hotelOrderMapper.updateHotelOrder(hotelOrder);
        if(hotelOrder.getStatus()==3){
            hotelOrderMapper.updateUserMembershipPoint(hotelOrder);
        }
        return i;
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteHotelOrderByIds(Long[] ids)
    {
        return hotelOrderMapper.deleteHotelOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteHotelOrderById(Long id)
    {
        return hotelOrderMapper.deleteHotelOrderById(id);
    }

    @Override
    public List<HotelOrder> selectAllRoom(HotelOrder order) {
        return hotelOrderMapper.selectAllRoom(order);
    }

    @Override
    public int updateOrderStatusByRoomNum(HotelOrder hotelOrder) {
        return hotelOrderMapper.updateOrderStatusByRoomNum(hotelOrder);
    }

    @Override
    public int completeOrder(HotelOrder hotelOrder) {
        Room room  = new Room();
        room.setId(hotelOrder.getId());
        room.setStatus(4L);
        roomMapper.updateRoom(room);
        HotelOrder order =  hotelOrderMapper.selectHotelOrderByRoom(hotelOrder);
        String[] split = order.getRnum().split(",");
        int i =  hotelOrderMapper.selectRoomCount(split);
        hotelOrder.setStatus(3L);
        if(i == split.length)
            return hotelOrderMapper.updateOrderStatusByRoomNum(hotelOrder);
        return 1;
    }

    @Override
    public Map<String, Object> selectOrderFinishingRate(HotelOrder order) {
         List<String> dates = generateLast3MonthsDates(true);
        List<Integer> all =   hotelOrderMapper.selectTrimesterOrder(order);
        order.setStatus(3L);
        List<Integer> success =  hotelOrderMapper.selectTrimesterOrder(order);
        return  Map.of("dates",dates,"success",success,"all",all);
    }

    @Override
    public List<Map<String, Object>> selectRoomTypeOderCount(HotelOrder order) {
        List<Map<String,Object>> list =  hotelOrderMapper.selectRoomTypeOderCount(order);

        return list;
    }


    public static List<String> generateLast3MonthsDates(boolean includeToday) {
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 获取当前日期（系统默认时区）
        LocalDate endDate = LocalDate.now();
        if (!includeToday) {
            endDate = endDate.minusDays(1); // 排除今天则结束日期为昨日
        }

        // 计算三个月前的日期（自动处理月末问题）
        // 示例：2023-10-05 → 2023-07-05
        // 示例：2023-03-31 → 2022-12-31（非闰年）/2022-12-30（自动调整）
        LocalDate startDate = endDate.minusMonths(1);

        // 生成日期列表
        List<String> dateList = new ArrayList<>();
        LocalDate currentDate = startDate;

        // 遍历直到结束日期（包含结束日期）
        while (!currentDate.isAfter(endDate)) {
            dateList.add(currentDate.format(formatter));
            currentDate = currentDate.plusDays(1);
        }

        return dateList;
    }
}
